import { Body, Controller, Get, Post } from "@nestjs/common";
import { ProfilesService } from "./profiles.service";
import type { CreateProfileDto } from "./interfaces/profiles.dto";
import { Public } from "src/decorators/is-public.decorator";
import { CustomizedCacheService } from "src/cache/cache.service";

@Controller("profiles")
export class ProfilesController {
    constructor(private readonly profilesService: ProfilesService, 
        private readonly cacheService: CustomizedCacheService) { }

    @Public()
    @Get()
    async findAll() {
        const cacheKey= `profiles_all`;
        
        if(await this.cacheService.has(cacheKey)) {
            return this.cacheService.get(cacheKey);
        }
        console.info('Not Hit in cache, fetching from DB');
        const profiles = await this.profilesService.findAll();
        return this.cacheService.set(cacheKey, profiles, 60 * 1000);
    }

    @Post('/insert')
    async createProfile(@Body() payload: CreateProfileDto) {
        return await this.profilesService.creaeteProfile(payload);
    }
}
